﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Gift.Model;
using Gift.DAL;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

namespace WebApp.admin.goods
{
    public partial class brand : BaseAdmin
    {
        tbBrand oBrand = new tbBrand();



        // Methods
        private void BrandBind()
        {
            try
            {
                DataTable brandById = BaseFactory.theBrand.GetBrandById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (brandById.Rows.Count > 0)
                {
                    this.txtBrandName.Value = brandById.Rows[0]["brandName"].ToString();
                    if (brandById.Rows[0]["brandLogo"].ToString() != "")
                    {
                        this.imgLogo.Src = brandById.Rows[0]["brandLogo"].ToString();
                        if (brandById.Rows[0]["brandLogo"].ToString() != "")
                        {
                            this.imgLogo.Style.Add("display", "block");
                        }
                        else
                        {
                            this.imgLogo.Style.Add("display", "none");
                        }
                    }
                    else
                    {
                        this.imgLogo.Style.Add("display", "none");
                    }
                    this.txtBrandUrl.Value = brandById.Rows[0]["brandUrl"].ToString();
                    this.txtaBrandDescription.Value = brandById.Rows[0]["brandDescription"].ToString();
                    this.txtSortOrder.Value = brandById.Rows[0]["sortOrder"].ToString();
                }
            }
            catch
            {
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                if (base.Request.QueryString["ot"] == "ed")
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.BrandEdit);
                }else
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.BrandAdd);
                }

                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    oBrand = BaseFactory.theBrand.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                }
                this.oBrand.brandName = this.txtBrandName.Value;
                if (this.fileBrandLogo.PostedFile.FileName != "")
                {
                    this.oBrand.brandLogo = "/cn/brand_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileBrandLogo.PostedFile.FileName);
                }
                this.oBrand.brandUrl = this.txtBrandUrl.Value;
                this.oBrand.brandDescription = this.txtaBrandDescription.Value;
                this.oBrand.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oBrand.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    DataTable brandById = BaseFactory.theBrand.GetBrandById(this.oBrand.id);
                    if (BaseFactory.theBrand.Update(this.oBrand))
                    {
                        if (this.fileBrandLogo.PostedFile.FileName != "")
                        {
                            if (((brandById.Rows.Count > 0) && (brandById.Rows[0]["brandLogo"].ToString() != "")) && File.Exists(base.Server.MapPath(brandById.Rows[0]["brandLogo"].ToString())))
                            {
                                File.Delete(base.Server.MapPath(brandById.Rows[0]["brandLogo"].ToString()));
                            }
                            this.fileBrandLogo.PostedFile.SaveAs(base.Server.MapPath(this.oBrand.brandLogo));
                        }
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='brandList.aspx';</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else
                {

                    this.oBrand.CompanyId = int.Parse(base.Administrator.CompanyId);
                    if (BaseFactory.theBrand.Add(this.oBrand) > 0)
                    {
                        if (this.fileBrandLogo.PostedFile.FileName != "")
                        {
                            this.fileBrandLogo.PostedFile.SaveAs(base.Server.MapPath(this.oBrand.brandLogo));
                        }
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");window.location.href='brandList.aspx';</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["ot"] == null)
                {
                    return builder;
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑品牌");
                    return builder;
                }
                builder.Append("添加品牌");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.BrandBind();
                }
                else if (base.Request.QueryString["ot"] == "ad")
                {
                    this.imgLogo.Style.Add("display", "none");
                }
            }
        }

    }
}